package com.kevms.phonewars;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/** 
* @class CreateMatchDM
* 
* @brief Activity for entering new Deathmatch information.
* 
* This Activity contains a table layout with numerous EditText
* views for entering new match information. 
*/ 
public class CreateMatchDM extends Activity {
	
	private Context mContext;
	private WebService mWebService = null;
	private JSONObject mJObject = null;
	
	EditText etMatchName;
	EditText etMaxPlayers;
	EditText etNumPlayers;
	EditText etGunRadius;
	EditText etGunRange;
	EditText etDmMaxPoints;
	
	private SQLiteDatabase db;
	private MySQLiteHelper dbHelper;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_match_dm);
		mContext = this;
		
		dbHelper = MySQLiteHelper.getInstance(getApplicationContext());
		db = dbHelper.getWritableDatabase();
		LinearLayout l = (LinearLayout)findViewById(R.id.topLayout);
    	l.setBackgroundColor(dbHelper.getBgColor(db));
		
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		etMatchName = (EditText)findViewById(R.id.etMatchName);
		etMaxPlayers = (EditText)findViewById(R.id.etMaxPlayers);
		etGunRadius = (EditText)findViewById(R.id.etGunRadius);
		etGunRange = (EditText)findViewById(R.id.etGunRange);
		etDmMaxPoints = (EditText)findViewById(R.id.etDmMaxPoints);
		
		mWebService = WebService.getInstance(mContext);
		
		try {
			JSONArray jArray = mWebService.getMatchTypeDefaults();
			for(int i=0;i<jArray.length();i++) {
				mJObject = jArray.getJSONObject(i);
				if(mJObject.getString("name").equals("Deathmatch")) {
					break;
				}
			}

			etMaxPlayers.setText(mJObject.getString("default_max_players"));
			etGunRadius.setText(mJObject.getString("default_gun_radius"));
			etGunRange.setText(mJObject.getString("default_gun_range"));
			etDmMaxPoints.setText("20");
		
		} catch (Exception e) {
			showAlert(e.getMessage());
		}
		
		Button btnCreateMatch = (Button)findViewById(R.id.btnCreateMatch);
		
		btnCreateMatch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(etMatchName.getText().toString().equals("")) {
					showAlert("Please enter a match name");
					return;
				}
				
				ArrayList<String> data = new ArrayList<String>();
				
				data.add(etMatchName.getText().toString());
				data.add(etMaxPlayers.getText().toString());
				data.add(etGunRadius.getText().toString());
				data.add(etGunRange.getText().toString());
				data.add(etDmMaxPoints.getText().toString());
				
				WebService webService = WebService.getInstance(mContext);
				try {
					webService.createMatchDM(data);
					CreateMatchDM.this.finish();
				} catch (Exception e) {
					showAlert(e.getMessage());
				}
			}
		});
	}
	
	private void showAlert(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(message)
			.setCancelable(false)
			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
		AlertDialog alert = builder.create();
		alert.show();
	}
}